﻿#pragma once
#include "Source.h"
#include <Iphlpapi.h>


class Internet: public Source
{
public:
    Internet();
    ~Internet() override
    {
        delete this->_pIfTable;
        delete this->_outSpeed;
        delete this->_inSpeed;
    }
    boolean doFrame() override
    {
        this->_invokeTimes += 1;
        if(this->_invokeTimes == this->_invokeFrame)
        {
            this->_invokeTimes = 0;
            const DWORD lastOutOctets = this->_lastOutOctets;
            const DWORD lastInOctets = this->_lastInOctets;
            this->loopTable();
            DWORD outDifference = this->_lastOutOctets - lastOutOctets;
            DWORD InDifference = this->_lastInOctets - lastInOctets;
            this->dealDifference(InDifference, outDifference);
            return true;
        }
        return false;
    }

    void setInvokeFrame(USHORT frame);
    USHORT getInvokeFrame();

    char* getOutSpeed() const;
    char* getInSpeed() const;
private:
    MIB_IFTABLE* _pIfTable = nullptr;
    DWORD _lastOutOctets = 0;
    DWORD _lastInOctets = 0;
    ULONG _dwSize = 0;
    USHORT _invokeFrame = 30;
    USHORT _invokeTimes = 0;
    char* _outSpeed = nullptr;
    char* _inSpeed = nullptr;
    void loopTable();
    void dealDifference(DWORD InDifference, DWORD outDifference) const;
};
